package com.likeycy.my.heap;

import java.util.Comparator;

/**
 * @ClassName: AbstractHeap
 * @Description: 抽象堆接口
 * @Author: sodagreen
 * @Date: 2021/3/27 3:59
 * @Version: 1.0
 */
public abstract class AbstractHeap<E> implements DiyHeap<E> {

    protected int size;
    protected Comparator<E> comparator;

    public AbstractHeap() {
        this(null);
    }

    public AbstractHeap(Comparator<E> comparator) {
        this.comparator = comparator;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    protected int compare(E e1, E e2) {
        if (comparator != null) {
            return comparator.compare(e1, e2);
        } else {
            return ((Comparable)e1).compareTo(e2);
        }
    }
}
